<template>
  <div class="app-container">
    <div
      class="filter-container"
      style="border: 1px solid #dcdcdc; padding: 20px; margin-bottom: 20px"
    >
      <el-link
        :type="listQuery.type == 0 ? 'primary' : ''"
        :underline="false"
        @click="tabChange(0)"
      >全部订单</el-link>
      <el-link
        :type="listQuery.type == 1 ? 'primary' : ''"
        :underline="false"
        style="margin-left: 60px"
        @click="tabChange(1)"
      >退款中</el-link>
      <el-link
        :type="listQuery.type == 2 ? 'primary' : ''"
        :underline="false"
        style="margin-left: 60px"
        @click="tabChange(2)"
      >维权完成</el-link>
    </div>
    <!-- 表格 -->
    <el-table
      :key="tableKey"
      ref="table"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column label="订单编号" prop="order_no" align="center" />
      <el-table-column label="商品id" align="center">
        <template slot-scope="{ row }">
          {{ row.product && row.product.id }}
        </template>
      </el-table-column>
      <el-table-column label="拍品" header-align="center" width="150">
        <template slot-scope="{ row }">
          <el-image
            v-if="row.product"
            style="width: 40px; height: 40px; text-align: center"
            :src="$store.state.settings.imgHost + row.product.image"
            :preview-src-list="[
              $store.state.settings.imgHost + row.product.image,
            ]"
          />
          <span
            v-if="row.product"
            style="vertical-align: top; margin-left: 10px"
          >{{ row.product.name }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="店铺" align="center">
        <template slot-scope="{ row }">
          {{
            (row.shopping && row.shopping.name) ||
              (row.shopping && row.shopping.shopping_name)
          }}
        </template>
      </el-table-column> -->
      <el-table-column label="申请账号" prop="user.user" align="center" />
      <el-table-column label="姓名" prop="id" align="center" />
      <el-table-column label="申请退款金额" align="center" prop="real_pay" />
      <el-table-column align="center" label="商品金额" prop="product.price" />
      <el-table-column
        align="center"
        label="买家服务费"
        prop="user_service_money"
      />
      <el-table-column
        align="center"
        label="卖家服务费"
        prop="seller_service_money"
      />
      <el-table-column align="center" label="状态">
        <template slot-scope="{ row }">
          {{
            row.status == 10
              ? "退款完成"
              : row.status == 11
                ? "退货"
                : row.status == 12
                  ? "退货中"
                  : ""
          }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="申请时间" prop="created_at" />
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="{ row }">
          <el-button
            v-if="row.status != 10"
            size="mini"
            plain
            type="success"
            @click="handleAgree(row.id)"
          >
            同意退款
          </el-button>
          <el-button
            v-if="row.status != 10"
            size="mini"
            plain
            type="success"
            @click="handleRefuse(row.id)"
          >
            拒绝
          </el-button>
          <el-button
            size="mini"
            plain
            type="success"
            @click="handleLook(row.id)"
          >
            订单详情
          </el-button>
          <el-button
            size="mini"
            plain
            type="success"
            @click="getKd(row.express_no)"
          >
            查看物流
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div v-if="total > 0" style="text-align: right">
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.per_page"
        @pagination="getList"
      />
    </div>

    <el-dialog title="查看物流" :visible.sync="dialogVisible" width="40%">
      <div v-loading="dialogLoading" style="line-height: 25px">
        <div
          v-for="(item, index) in kdInfoList"
          :key="index"
          style="margin-bottom: 30px"
        >
          <div>{{ item.time }}</div>
          <div>{{ item.status }}</div>
        </div>
        <div v-if="kdInfoList && kdInfoList.length <= 0">没有物流信息</div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getOrdersaftersale, updateOrders, getKdInfoList } from '@/api/common'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'OrdersAfterSale',
  components: {
    Pagination
  },
  directives: {
    waves
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      dialogVisible: false,
      form: {},
      listQuery: {
        page: 1,
        per_page: 20,
        //   type: 0
        source: 7
      },
      kdInfoList: undefined,
      dialogLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    clearFilter() {
      this.listQuery['type'] = 0
      this.handleFilter()
    },
    tabChange(e) {
      this.listQuery.type = e
      this.getList()
    },
    getKd(express_no) {
      this.dialogVisible = true
      this.dialogLoading = true
      getKdInfoList(express_no)
        .then((res) => {
          this.kdInfoList = res.data.result.list
        })
        .finally(() => {
          this.dialogLoading = false
        })
    },
    getList() {
      this.listLoading = true
      getOrdersaftersale(this.listQuery)
        .then((response) => {
          const { data, total } = response.data
          this.list = data
          this.total = total
        })
        .finally(() => {
          this.listLoading = false
        })
    },
    // 拒绝
    handleRefuse(id) {
      const that = this
      this.$prompt('', '拒绝', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /\S/,
        inputPlaceholder: '请输入拒绝原因',
        inputErrorMessage: '拒绝原因不能为空',
        inputType: 'textarea',
        closeOnPressEscape: false,
        closeOnClickModal: false,
        closeOnHashChange: false,
        callback() {},
        beforeClose(action, instance, done) {
          if (action === 'confirm') {
            if (instance.confirmButtonLoading) {
              return false
            }
            instance.confirmButtonLoading = true
            instance.confirmButtonText = '操作中...'
            updateOrders(id, {
              refund_status: '0',
              content: instance.inputValue
            })
              .then(() => {
                that.getList()
                that.$notify({
                  title: '提示',
                  message: '操作成功',
                  type: 'success',
                  duration: 2000
                })
                done()
              })
              .finally(() => {
                instance.confirmButtonLoading = false
                instance.confirmButtonText = '确定'
              })
          } else {
            if (!instance.confirmButtonLoading) {
              done()
            } else {
              that.$message.warning('操作中，禁止关闭')
            }
          }
        }
      })
    },
    handleAgree(id) {
      this.$confirm('请确认是否同意退款？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          updateOrders(id, { refund_status: '1' }).then(() => {
            this.getList()
            this.$notify({
              title: '提示',
              message: '操作成功',
              type: 'success',
              duration: 2000
            })
          })
        })
        .catch(() => {})
    },
    // 查看
    handleLook(row) {
      //   this.$router.push("/member/detail/" + row);
      this.$router.push('/orders/detail-sc/' + row)
    }
  }
}
</script>
